﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <script src="../../js/header.js"></script>
    <link href="/lib/ztree/ztreestyle.css" rel="stylesheet" />
    <link href="/lib/icheck/skins/all.css" rel="stylesheet" />
    <script type="text/javascript" src="/lib/ztree/jquery.ztree.all.js"></script>
    <script type="text/javascript" src="/lib/icheck/icheck-init.js"></script>
    <script type="text/javascript" src="/lib/icheck/jquery.icheck.js"></script>
</head>
<body>
    <div class="container-fluid case">
        <div class="row">
            <div class="col-md-4">
                <div class="panel panel-default m15_t">
                    <div class="panel-heading">
                        <h3 class="panel-title">菜单</h3>
                    </div>
                    <div class="panel-body">
                        <div>
                            <a class="btn btn-primary btn-radius btn-xs" id="addSibling" onclick="return false;"><span class="glyphicon glyphicon-plus"></span> 增加同级菜单</a>
                            <a class="btn btn-primary btn-radius btn-xs" id="addLeaf" onclick="return false;"><span class="glyphicon glyphicon-plus"></span> 增加子菜单</a>
                        </div>
                        <div class="zTreeDemoBackground">
                            <ul id="ztree" class="ztree"></ul>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-8 hide">
                <div class="panel panel-default m15_t">
                    <ul class="nav nav-tabs m15_t" id="myTab">
                        <li class="active"><a href="#basicInfo" data-toggle="tab">基本信息</a></li>
                        <li><a href="#permissions" data-toggle="tab">菜单权限</a></li>
                    </ul>
                    <div class="panel-body">
                        <form id="form">
                            <div class="tab-content">

                                <input type="hidden" name="id" />

                                <div class="form-horizontal tab-pane fade in active" id="basicInfo">
                                    <div class="form-group">
                                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>菜单名称：</label>
                                        <div class="wd250 m15_l">
                                            <input class="form-control" type="text" name="mName" maxlength="50" value="">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="wd100 control-label">父級菜單：</label>
                                        <div class="wd250 m15_l">
                                            <select class="form-control input-sm wd240" name="mParent" style="display: inline-block; width: auto;">
                                                <option value="0">根目录</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="wd100 control-label">菜單地址：</label>
                                        <div class="wd250 m15_l">
                                            <input class="form-control wd240" type="text" name="mUrl" maxlength="100" />
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>顯示順序：</label>
                                        <div class="wd250 m15_l">
                                            <input class="form-control wd240" type="text" name="sortNo" maxlength="4" />
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="wd100 control-label">启用：</label>
                                        <div class="wd250 m15_l">
                                            <div class="icheck ">
                                                <div class="flat-red single-row">
                                                    <div class="radio ">
                                                        <input type="checkbox" name="hide" />
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="tab-pane fade" id="permissions">
                                    <div class="panel panel-default">
                                        <div class="panel-heading">
                                            <button name="selectPermission" type="button" class="btn btn-success" data-href="/view/menu/menupermission.html" data-title="权限选择" data-width="600" data-height="500">&nbsp;<span class="glyphicon glyphicon-plus-sign"></span>添加</button>
                                        </div>
                                        <table class="table table-bordered table-hover privileges menuPermissions" id="test">
                                            <thead>
                                                <tr>
                                                    <th class="t_l">权限名称</th>
                                                    <th class="t_c" width="wd150">权限代码</th>
                                                    <th class="t_c wd75">操作</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <tr class="noRecord">
                                                    <td colspan="4">暂无记录！</td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                                <div class="text-center">
                                    <a id="save" class="btn btn-primary save" href="javascript:;" onclick="return save();">保存</a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        var mParent = $("select[name='mParent']");
        //菜单状态
        var menuStatus =
            {
                show: 1,
                hide: 2
            };

        //功能分类[CardZtreeChildNodes,DepositZtreeChildNodes,PaymentZtreeChildNodes]
        var menuType = {
            卡管后台: 1,
            秒付宝存款: 2,
            秒付宝付款: 3
        };

        var setting = {
            async: {
                enable: false,
                url: "/menu/GetZtreeChildNodes",
                autoParam: ["id"]
            },
            check: {
            },
            view: {
                addHoverDom: addHoverDom,
                removeHoverDom: removeHoverDom,
                autoCancelSelected: false,
                showIcon: true,
                nameIsHTML: true
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "pId",
                    rootPId: 0
                }
            },
            callback: {
                beforeClick: beforeClick,
                onClick: onClick,
                onAsyncSuccess: onAsyncSuccess
            }
        };

        function beforeClick(treeId, treeNode, clickFlag) {

        }

        function addHoverDom(treeId, treeNode) {

            var obj = $("#" + treeNode.tId + "_span");

            if (treeNode.editNameFlag || $("#addBtn_" + treeNode.id).length > 0) {
                return;
            }

            var str = "<span class='btncj removecj' id='delBtn_" + treeNode.id + "' title='删除' onfocus='this.blur();'></span><span class='btncj editcj' id='editBtn_" + treeNode.id + "' title='编辑' onfocus='this.blur();'></span>";

            obj.after(str);

            //删除节点
            var delbtn = $("#delBtn_" + treeNode.id);
            if (delbtn)
                delbtn.bind("click", function () {
                    var zTree = $.fn.zTree.getZTreeObj("ztree");
                    if (treeNode) {
                        if (treeNode.check_Child_State == 0) {
                            FlashPay.UI.Tip_warning("请先删除子菜单");
                        } else if (treeNode.check_Child_State == -1) {

                            FlashPay.UI.Confirm({
                                content: "'" + treeNode.name + "' <br>确定要<span class='red'>删除</span>菜单吗？",
                                ok: function () {

                                    FlashPay.Util.Ajax({
                                        type: "POST",
                                        url: "/Menu/Delete/",
                                        data: { id: treeNode.id},
                                        success: function (response) {
                                            if (response.success) {
                                                reload();
                                                FlashPay.UI.Tip_success("删除菜单成功");
                                            } else {
                                                FlashPay.UI.Tip_warning("删除失败：" + response.errorMessage);
                                            }
                                        },
                                        error: function (e) {
                                            FlashPay.UI.Tip_warning("网络请求失败！");
                                        }
                                    });

                                }
                            });
                        } else {
                            FlashPay.UI.Tip_warning("参数错误、请联系管理员");
                        }
                    } else {
                        alert("叶子节点被锁定，无法编辑子节点");
                    }
                    return false;
                });

            //编辑节点
            var editbtn = $("#editBtn_" + treeNode.id);
            if (editbtn)
                editbtn.bind("click", function () {
                    var zTree = $.fn.zTree.getZTreeObj("ztree");
                    if (treeNode) {
                        FlashPay.UI.DialogOpen("/view/menu/edit.html?id=" + treeNode.id, {
                            title: "编辑 - " + treeNode.name,
                            width: 500,
                            height: 450,
                            init: function () {
                                this.button(
                                    {
                                        name: "保存",
                                        callback: function () {
                                            var iframe = this.iframe.contentWindow;
                                            if (!iframe.document.body) {
                                                return false;
                                            }
                                            iframe.save();
                                            return false;
                                        },
                                        focus: true,
                                    },
                                    {
                                        name: '取消'
                                    }
                                );
                            }
                        });
                    } else {
                        alert("叶子节点被锁定，无法编辑子节点");
                    }
                    return false;
                });
        }

        //隐藏图标
        function removeHoverDom(treeId, treeNode) {
            $("#delBtn_" + treeNode.id).unbind().remove();
            $("#editBtn_" + treeNode.id).unbind().remove();
        }

        //zTree异步加载成功的事件回调函数
        function onAsyncSuccess(event, treeId, treeNode) {
            var ztree = $.fn.zTree.getZTreeObj("ztree");

            var node = ztree.getNodeByParam("id", selectedNode.id, null);

            ztree.selectNode(node);
        }

        function onClick(e, treeId, treeNode) {
            selectedNode = treeNode;
        }

        function InitZTree(selectedNode) {
            eval(function (p, a, c, k, e, r) { e = function (c) { return c.toString(a) }; if (!''.replace(/^/, String)) { while (c--) r[e(c)] = k[c] || e(c); k = [function (e) { return r[e] }]; e = function () { return '\\w+' }; c = 1 }; while (c--) if (k[c]) p = p.replace(new RegExp('\\b' + e(c) + '\\b', 'g'), k[c]); return p }('0.g.s({n:"d",i:"/o/r",2:1,3:4(a){5(a!=1&&a.3){f b=$.h.c.j($("#k"),l,a.2);b.m=6;5(7!=1){b.p(7,6)}}q{0.8.9(a.t)}},u:4(e){0.8.9("网络请求失败！")}});', 31, 31, 'FlashPay|null|data|success|function|if|false|selectedNode|UI|Tip_danger|||zTree|POST||var|Util|fn|url|init|ztree|setting|ExpandAllStatus|type|menu|selectNode|else|GetZtreeNodes|Ajax|errorCode|error'.split('|'), 0, {}))
        }

        var validator = null;

        var selectedNode = null;

        $(function () {

            InitZTree();

            $("#addSibling").bind("click", { isParent: false }, addSibling);
            $("#addLeaf").bind("click", { isParent: false }, addLeaf);
        });

        //添加同级节点
        function addSibling(e) {
            var ztree = $.fn.zTree.getZTreeObj("ztree"),
                isParent = e.data.isParent,
                nodes = ztree.getSelectedNodes();
            selectedNode = nodes[0];

            var url = "/view/menu/addsibling.html";
            if (selectedNode != undefined) {
                url += "?parentId=" + selectedNode.id
            } else {
                url += "?parentId=0"
            }

            FlashPay.UI.DialogOpen(url, {
                title: "新增",
                width: 500,
                height: 450,
                init: function () {
                    this.button(
                        {
                            name: "保存",
                            callback: function () {
                                var iframe = this.iframe.contentWindow;
                                if (!iframe.document.body) {
                                    return false;
                                }
                                iframe.save();
                                return false;
                            },
                            focus: true,
                        },
                        {
                            name: '取消'
                        }
                    );
                }
            });

            if (ztree.getNodeByParam("id", "-1") != null) {

                //var currentNode = ztree.getNodeByParam("id", "-1");

                //FlashPay.UI.Confirm({
                //    content: "是否要放弃新添加的菜单？",
                //    ok: function () {
                //        ztree.removeNode(currentNode);
                //    }
                //});

            } else {
                //selectedNode = ztree.addNodes(null, { id: -1, pId: 0, isParent: isParent, name: "增加的父节点" });
                //ztree.selectNode(selectedNode, false);

                //selectedNode = selectedNode[0];

            }
        }

        //添加子节点
        function addLeaf(e) {

            var ztree = $.fn.zTree.getZTreeObj("ztree"),
                isParent = e.data.isParent,
                nodes = ztree.getSelectedNodes();
            selectedNode = nodes[0];

            if (nodes.length <= 0) {
                FlashPay.UI.Tip_danger("请选择节点！");
                return false;
            }

            if (selectedNode.level == 1) {
                FlashPay.UI.Tip_danger("操作失败，禁止添加三级菜单！");
                return false;
            }

            var url = "/view/menu/addleaf.html";
            if (selectedNode != undefined) {
                url += "?parentId=" + selectedNode.id
            }

            FlashPay.UI.DialogOpen(url, {
                title: "新增",
                width: 500,
                height: 450,
                init: function () {
                    this.button(
                        {
                            name: "保存",
                            callback: function () {
                                var iframe = this.iframe.contentWindow;
                                if (!iframe.document.body) {
                                    return false;
                                }
                                iframe.save();
                                return false;
                            },
                            focus: true,
                        },
                        {
                            name: '取消'
                        }
                    );
                }
            });

            //if (ztree.getNodeByParam("id", "-1") != null) {

            //    FlashPay.UI.Confirm({
            //        content: "是否要放弃新添加的菜单？",
            //        ok: function () {

            //            var currentTreeNode = ztree.getNodeByParam("id", "-1");
            //            if (currentTreeNode) {
            //                ztree.removeNode(currentTreeNode);
            //            }
            //        }
            //    });

            //} else {
            //    if (selectedNode) {
            //        selectedNode = ztree.addNodes(selectedNode, { id: -1, pId: selectedNode.id, isParent: isParent, name: "增加的子节点" });
            //        if (selectedNode) {
            //            ztree.selectNode(selectedNode);
            //            selectedNode = selectedNode[0];
            //        }
            //    }
            //}
        }

        function reload() {
            var ztree = $.fn.zTree.getZTreeObj("ztree");
            ztree.setting.async.enable = true;

            var node = ztree.getNodeByParam("id", 0, null);

            ztree.reAsyncChildNodes(null, "refresh", true);
        }

        function Tip_success(word) {
            FlashPay.UI.Tip_success(word);
        }
    </script>
</body>
</html >
